#!/usr/bin/python
# -*- coding: utf-8 -*-


from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from openpolitics.models import Person, Deputy, Elections
from promisecheck.models import Promise, Status, Log, PromiseForm, LogForm
from promisecheck.utils import promises_list

import string

dbs = {
  'default': 'default',
  'openpolitics': 'default'
}

def index(request,
          template_name='promisecheck'):
    return render_to_response('%s/index.html' % template_name, {
    }, context_instance=RequestContext(request))

def people(request,
          template_name='promisecheck'):
    election = Elections.objects.using(dbs['openpolitics']).get(id=1)
    pol = Deputy.objects.filter(pk__in=election.personcandidate_set.values_list('candidate'))
    letters = string.ascii_uppercase
    list = [(letter, pol.filter(last_name__startswith=letter).order_by('last_name')) for letter in letters]

    return render_to_response('%s/people.html' % template_name, {
        'list': list,
    }, context_instance=RequestContext(request))

def promises(request,
             template_name='promisecheck'):
    people = Person.objects.all()
    pr = Person.objects.get(terms__position__type__name='President',
                            terms__position__institution__name='Rzeczpospolita Polska')
    pr.promise_list = promises_list(pr)
    pm = Person.objects.get(terms__position__type__name='Prime minister',
                            terms__position__institution__name='Rząd')
    pm.promise_list = promises_list(pm)
    items=[]
    row = []
    row.append(('Minister Obrony Narodowej', pm))
    row.append(('Minister Zdrowia', pm))
    row.append(('Minister Kultury', pm))
    items.append(row)
    row = []
    row.append(('Minister Gospodarki', pm))
    row.append(('Minister Infrastruktury', pm))
    row.append(('Minister Rolnictwa', pm))
    items.append(row)
    return render_to_response('%s/promises.html' % template_name, {
        'people': people,
        'pr': pr,
        'pm': pm,
        'items': items,
    }, context_instance=RequestContext(request))

def person(request,
           slug,
           template_name='promisecheck'):
    person = Deputy.objects.using(dbs['openpolitics']).get(slug=slug)
    person.promise_list = promises_list(person)
    statuses = Status.objects.all()
    log = Log.objects.filter(promise__person=person)[:5]
    return render_to_response('%s/person.html' % template_name, {
        'person': person,
        'log': log,
        'statuses': statuses,
    }, context_instance=RequestContext(request))

def cycles(request,
           template_name='promisecheck'):
    el = Elections.objects.all()
    list = (('2010', el),)
    return render_to_response('%s/cycles.html' % template_name, {
        'list': list,
        'len': el[0].personcandidate_set.count(),
    }, context_instance=RequestContext(request))

def cycle(request, cid,
          template_name='promisecheck'):
    election = Elections.objects.using(dbs['openpolitics']).get(id=cid)
    people = Deputy.objects.filter(personcandidate__elections=election)
    for person in people:
        person.promise_list = promises_list(person)
    return render_to_response('%s/cycle.html' % template_name, {
        'cycle': cycle,
        'people': people,
    }, context_instance=RequestContext(request))

def promise_list(request,
                 person,
                 status,
                 filter=None,
                 template_name='promisecheck'):
    if not status:
        pol = Promise.objects.filter(person__slug=person)
    else:
        pol = Promise.objects.filter(person__slug=person, status__name=status)
    letters = string.ascii_uppercase
    list = [(letter, pol.filter(name__startswith=letter).order_by('name')) for letter in letters]
    return render_to_response('%s/promise_list.html' % template_name, {
        'list': list,
    }, context_instance=RequestContext(request))

def promise(request,
            promise,
            template_name='promisecheck'):
    po = Promise.objects.get(pk=promise)
    log = po.logs.all()
    return render_to_response('%s/promise.html' % template_name, {
        'po': po,
        'log': log,
    }, context_instance=RequestContext(request))

@login_required
def add_promise(request,
                person,
                form=None,
                template_name='promisecheck'):
    if form is None:
        form = PromiseForm()
    return render_to_response('%s/add_promise.html' % template_name, {
        'form': form,
        'person': person,
    }, context_instance=RequestContext(request))

@login_required
def update_promise(request,
                   promise,
                   form=None,
                   template_name='promisecheck'):
    promise = Promise.objects.get(pk=promise)
    log = Log(status=promise.status)
    if form is None:
        form = LogForm(instance=log)
    return render_to_response('%s/update_promise.html' % template_name, {
        'form': form,
        'promise': promise.id,
    }, context_instance=RequestContext(request))

